package com.ruoyi.finance.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.finance.domain.Account;
import com.ruoyi.finance.domain.ChargeRecord;
import com.ruoyi.finance.domain.CzHistory;
import com.ruoyi.finance.domain.Merchant;
import com.ruoyi.finance.mapper.ChargeRecordMapper;
import com.ruoyi.finance.mapper.CzHistoryMapper;
import com.ruoyi.finance.mapper.MerchantMapper;
import com.ruoyi.finance.service.IMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 商户信息Service业务层处理
 *
 * @author zt
 * @date 2023-11-19
 */
@Service
public class MerchantServiceImpl implements IMerchantService
{
    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private ChargeRecordMapper chargeRecordMapper;

    @Autowired
    private CzHistoryMapper czHistoryMapper;


    /**
     * 查询商户信息(单个)
     * @param merchant
     * @return
     */

    @Override
    public Merchant selectMerchantByUserOne(Merchant merchant)
    {
        return merchantMapper.selectMerchantByUserOne(merchant);
    }

    /**
     * 查询商户信息列表
     *
     * @param merchant 商户信息
     * @return 商户信息
     */
    @Override
    public List<Merchant> selectMerchantList(Merchant merchant)
    {
        return merchantMapper.selectMerchantList(merchant);
    }

    /**
     * 新增商户信息
     *
     * @param merchant 商户信息
     * @return 结果
     */
    @Override
    public int insertMerchant(Merchant merchant)
    {
        merchant.setCreateTime(DateUtils.getNowDate());
        return merchantMapper.insertMerchant(merchant);
    }

    /**
     * 修改商户信息
     *
     * @param merchant 商户信息
     * @return 结果
     */
    @Override
    public int updateMerchant(Merchant merchant)
    {
        merchant.setUpdateTime(DateUtils.getNowDate());
        return merchantMapper.updateMerchant(merchant);
    }

    @Override
    @Transactional
    public int debit(Merchant merchant, ChargeRecord cr) {
        // 扣费
        int reduceBalance = merchantMapper.reduceBalance(merchant);
        // 增加扣费记录
        int record = chargeRecordMapper.insertChargeRecord(cr);
        if (reduceBalance != 1 || record != 1){
            //回滚
            return reduceBalance + record;
        }
        return reduceBalance + record;
    }

    @Override
    public List<Account> selectAccountsList(Account account) {
        return merchantMapper.selectAccountsList(account);
    }

    @Override
    @Transactional
    public int addBalanceByDuigong(CzHistory czh) {
        // 增加对公充值记录
        czh.setCzDate(new Date());
        int i = czHistoryMapper.insertCzHistory(czh);
        int addBalance = merchantMapper.addBalance(new Merchant(czh.getJsczUserId(), czh.getEdu()));
        if (i != 1 || addBalance != 1){
            //回滚
            return i + addBalance;
        }
        return i + addBalance;
    }

}
